using StackExchange.Redis;
using System.Net;

namespace 连接redis
{
    public partial class Form1 : Form
    {

        IDatabase db;

        public Form1()
        {
            InitializeComponent();

            var connectionString = "localhost"; // Redis服务器地址  
            var port = 6379; // Redis服务器端口号

            var configuration = new ConfigurationOptions
            {
                // 只读的setter属性，创建对象的时候进行一次设置
                // 使用双大括号，用来初始化集合
                EndPoints = { { connectionString, port } }, // 指定服务器地址和端口号  
                // Password = "your_password" // 如果需要身份验证，设置密码  
            };

            ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(configuration); // 创建连接实例  
            db = connection.GetDatabase(); // 获取数据库实例
        }
        /// <summary>
        /// 连接redis
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(db.ToString());
        }


        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            // 设置键值对  
            db.StringSet(textBox1.Text, textBox2.Text);
        }
        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            // 获取键值对  
            string value = db.StringGet(textBox3.Text);
            textBox4.Text = value;
        }
    }
}
